<?php

namespace common\models\user;

use Yii;
use yii\helpers\ArrayHelper;
use common\helpers\Url;

/**
 * This is the model class for table "{{%user}}".
 *
 * @property int $id
 * @property string $username
 * @property string $nickname
 * @property string $mobile
 * @property string $mobile_hash
 * @property string $email
 * @property int $gender
 * @property string $avatar 头像
 * @property string $auth_key
 * @property string $password_hash
 * @property string $password_reset_token
 * @property int $ip
 * @property int $status
 * @property string $created_at
 * @property string $updated_at
 * @property string $deleted_at
 */
class User extends \common\base\User {

    use \common\traits\Constants;

    const STATUS_DELETED = -1;
    const STATUS_DEFAULT = 0;
    const STATUS_ACTIVE = 10;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%user}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['username', 'auth_key'], 'required'],
            [['gender', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['username', 'nickname', 'mobile', 'auth_key'], 'string', 'max' => 32],
            [['email'], 'string', 'max' => 50],
            [['mobile_hash', 'avatar', 'password_hash', 'password_reset_token'], 'string', 'max' => 255],
            [['username'], 'unique'],
            [['mobile'], 'unique'],
            [['password_reset_token'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'username' => '用户名',
            'nickname' => '昵称',
            'mobile' => '手机号',
            'mobile_hash' => '加密手机号',
            'email' => 'Email',
            'avatar' => '头像',
            'gender' => '性别',
            'auth_key' => 'Auth Key',
            'password_hash' => 'Password Hash',
            'password_reset_token' => 'Password Reset Token',
            'ip' => '注册IP',
            'status' => '状态',
            'created_at' => '注册时间',
            'updated_at' => '更新时间',
            'deleted_at' => '注销时间',
        ];
    }

    public function getMeta() {
        return $this->hasOne(UserMeta::class, ['user_id' => 'id']);
    }

    public function getDefaultAvatar() {
        return ArrayHelper::getValue(Yii::$app->params, ['defaultImage', 'avatar'], static::DEFAULT_AVATAR);
    }

    public function getPortrait() {
        $avatar = $this->avatar ?: $this->getDefaultAvatar();
        if (Url::isRelative($avatar)) {
            $avatar .= '?x-oss-process=image/resize,m_fill,h_100,w_100';
        }
        return Url::attach($avatar);
    }

    public function getActived() {
        return $this->status == static::STATUS_ACTIVE;
    }

    public function getUserName() {
        if (!$this->actived) {
            return '匿名用户';
        }
        if ($this->nickname) {
            return $this->nickname;
        }
        return $this->username;
    }

    public function getDescription() {
        if (!$this->actived) {
            return '系统需要您的注册信息';
        }
        if ($this->meta && $this->meta->signature) {
            return $this->meta->signature;
        }
        return '这家伙很懒，什么都没有写~';
    }

    public function fields() {
        return [
            'id',
            'username' => 'userName',
            'email',
            'avatar' => 'portrait',
        ];
    }

}
